package com.wyix;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

public class GameStartDemo {
    // 定义需要等待的玩家数量
    private static final int PLAYER_COUNT = 5;
    // 创建CountDownLatch，计数器初始化为玩家数量
    private static final CountDownLatch LATCH = new CountDownLatch(PLAYER_COUNT);
    private static final Random RANDOM = new Random();

    public static void main(String[] args) throws InterruptedException {
        System.out.println("[系统] 等待玩家准备...");

        // 启动5个玩家线程
        for (int i = 1; i <= PLAYER_COUNT; i++) {
            final String playerName = "玩家" + i;
            new Thread(() -> {
                try {
                    // 模拟玩家准备时间（500-2000ms随机）
                    int prepareTime = RANDOM.nextInt(1500) + 500;
                    Thread.sleep(prepareTime);
                    System.out.println("[通知] " + playerName + " 准备完成！(耗时" + prepareTime + "ms)");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    System.out.println("[警告] " + playerName + " 准备被中断");
                    return;
                }
                // 玩家准备完成后，计数器减1
                LATCH.countDown();
            }).start();
        }

        // 主线程等待所有玩家准备完成（计数器归零）
        LATCH.await();
        System.out.println("\n[系统] 所有玩家准备完毕！游戏正式开始！\n🎮 加载地图...\n👪 生成角色...\n⚔ 开始对战！");
    }
}